package com.junmoo.www.module.system.controller;

import com.junmoo.www.module.system.entity.SysResource;
import com.junmoo.www.module.system.entity.SysRole;
import com.junmoo.www.module.system.service.SysResourceService;
import com.junmoo.www.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/resource")
@Slf4j
public class SysResourceController {
    @Autowired
    SysResourceService sysResourceService;
    @GetMapping("/findAll")
    public R findAll(){
        return R.success("success",sysResourceService.findAll());
    }

    @GetMapping("/page")
    public R page(SysResource search, Integer currentPage, Integer pageSize,
                  @RequestParam(defaultValue = "resourceCode") String sortField, @RequestParam(defaultValue = "asc")String sortOrder){
        Sort.Direction direction= sortOrder.toLowerCase().contains("asc")?Sort.Direction.ASC:Sort.Direction.DESC;
        Pageable pageable = PageRequest.of(currentPage - 1, pageSize,Sort.by(direction,sortField));
        Page<SysResource> page =  sysResourceService.page( search, pageable);
        return R.success(page);
    }

    @PostMapping()
    public R create(@RequestBody SysResource entity){
        return  sysResourceService.create( entity);
    }

    @PutMapping()
    public R edit(@RequestBody SysResource entity){
        return  sysResourceService.edit( entity);
    }

    @DeleteMapping()
    public R deleteByIds(@RequestBody List<Number> ids){
        return  sysResourceService.deleteByIds( ids);
    }
}
